const {sqlRows, rows, save, sqlUpdate, sqlDelete} = require("../../sql/rows");
const handlerList = require("../../handler/handlerList");
const {format} = require("../../utils");
const users = require("express").Router()
const config = require("../../config")
const {rules, valid} = require("../../utils/rules");

const usersSave = [
    {
        name: "username",
        message: "用户名"
    },
    {
        name: "icon",
        message: "头像"
    },
    {
        name: "email",
        message: "邮箱"
    },
    {
        name: "nick_name",
        message: "昵称"
    },
    {
        name: "user_phone",
        message: "手机号"
    },
]
const usersUpdate = [].concat(usersSave, [
    {
        name: "id",
        message: "id"
    },
])
// 查询所有用户
users.get('/queryList', async (req, res) => {
    try {
        const sql = sqlRows('ums_user',{data: req.query})
        const user = await req.db.query(sql)
        return res.status(200).send({
            code: 200,
            message: '',
            data: user
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})
// 查询列表
users.post("/list", (req, res) => handlerList(req ,res, 'ums_user'))
// 添加
users.post("/save",rules(usersSave), valid, async (req, res) => {
    try {
        const user = await req.db.query(rows('ums_user'))
        const {
            username
        } = req.body
        if (user.length) {
            const [row] = user.filter(item => item.username === username)
            if (row) {
                return res.status(200).send({
                    code: 400,
                    message: "用户名重复无法添加",
                    data: ""
                })
            }
        }
        const data = {
            ...req.body,
            create_time: format(),
            login_time: format(),
            password: config.password,
            status: 1
        }
        await req.db.query(save("ums_user", data))
        return res.status(200).send({
            code: 200,
            message: '添加成功',
            data: ''
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})
// 编辑
users.post("/update", rules(usersUpdate), valid, async (req, res) => {
    try {
        const {
            username,
            icon,
            email,
            nick_name,
            user_phone,
            id
        } = req.body
        const data = {
            username,
            icon,
            email,
            nick_name,
            user_phone,
        }
        const sql = sqlUpdate('ums_user', {data, key:{id}})
        await req.db.query(sql)
        return res.status(200).send({
            code: 200,
            message: '修改成功',
            data: ''
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }

})
// 删除
users.delete("/delete", async (req, res) => {
    try {
        if (req.query.id) {
            const id = req.query.id
            const sql = sqlRows('ums_role_or_user', {data: {userId: id}})
            const user = await req.db.query(sql)
            if (user.length) {
                const data = {
                    userId: id
                }
                await req.db.query(sqlDelete('ums_role_or_user', {data}))
            }
            const u = await req.db.query(sqlRows('ums_user', {data: {id}}))
            if (u) {
                await req.db.query(sqlDelete('ums_user', {data:{id}}))
                return res.status(200).send({
                    code: 200,
                    data: "",
                    message: "删除成功"
                })
            }
            return res.status(200).send({
                code: 400,
                message: '当前列表没有该用户',
                data: ''
            })
        }
        return res.status(200).send({
            code: 400,
            message: '缺少用户id',
            data: ''
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})
// 状态修改
users.post("/status", async (req, res) => {
    try {
        const {
            status,
            id
        } = req.body
        const data = {
            status
        }
        const sql = sqlUpdate('ums_user', {data, key:{id}})
        await req.db.query(sql)
        return res.status(200).send({
            code: 200,
            message: '状态修改成功',
            data: ''
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})



module.exports = users